<?php

class ArticleController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	
    	$identity= Zend_Auth::getInstance()->getIdentity();
    	$role=$identity->role;
    	if ($role=='admin'){
    		$layout=$this->_helper->layout();
    		$layout->setLayout('adminlayout');
    	}
    }

    public function indexAction()
    {
        $articles = new Application_Model_DbTable_Articles();
    	$this->view->articles = $articles->fetchAll();
    }

    public function addAction()
    {
     $form = new Application_Form_Article();
    $form->envoyer->setLabel('Ajouter');
    $this->view->form = $form;

    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
        	$upload = new Zend_File_Transfer_Adapter_Http();
        	$upload ->setDestination('/Applications/MAMP/htdocs/uploads/files/images');
        	try {
        		$upload->receive();
        		$image=$upload->getFileName();
        		
        	} catch (Zend_File_Transfer_Exception $e) {
        		$e->getMessage();
        	}
        	//$uploadedfile=$form->getValidValues('image');
        	//Zend_Debug::dump($uploadedfile);
        	
        	
        	$reference=$form->getValue('reference');
        	$gencod=$form->getValue('gencod');
        	$statut=$form->getValue('statut');
        	$category=$form->getValue('category');
        	$marque=$form->getValue('marque');
        	//$image=$form->getValue('image');
        	$designation = $form->getValue('designation');
        	$description=$form->getValue('description');
            $price = $form->getValue('price');
            
            $articles = new Application_Model_DbTable_Articles();
            $articles->addArticle($reference,$gencod,$statut,$category,$marque,$image,$designation,$description, $price);

            $this->_helper->redirector('index');
        } else {
            $form->populate($formData);
        }
    }
    }

    public function updateAction()
    {
    $form = new Application_Form_Article();
    $form->envoyer->setLabel('Sauvegarder');
    $this->view->form = $form;

    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
            $id = $form->getValue('id');
            $designation = $form->getValue('designation');
            $price = $form->getValue('price');
            $articles = new Application_Model_DbTable_Articles();
            $articles->updateArticle($id, $designation, $price);

            $this->_helper->redirector('index');
        } else {
            $form->populate($formData);
        }
    } else {
        $id = $this->_getParam('id', 0);
        if ($id > 0) {
            $articles = new Application_Model_DbTable_Articles();
            $form->populate($articles->getArticle($id));
        }
    }
    }

    public function deleteAction()
    {
     
        
            $id = $this->getRequest()->getParam('id',0);
            $articles = new Application_Model_DbTable_Articles();
            $articles->deleteArticle($id) ;

       		$this->_helper->redirector('index');
   
    }
    
    public function detailsAction()
    {
    	$id=$this->getRequest()->getParam('id',0);
    	$articles=new Application_Model_DbTable_Articles();
    	$article= $articles->getArticle($id);
    	$idMarque= (int)$article['idMarque'];
    	$idCategory= (int)$article['idCategorie'];
    	$marques = new Application_Model_DbTable_Marques();
    	$marque= $marques->getMarque($idMarque);
    	$categories = new Application_Model_DbTable_Categories();
    	$categorie= $categories->getCategorie($idCategory);
    	$this->view->article = $article;
    	$this->view->marque = $marque;
    	$this->view->category = $categorie;
    }


}







